//
//  SEGDoorCollViewCell.m
//  SEGUhomeIntelligentHardware
//
//  Created by MOON FLOWER on 2018/7/5.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGDoorCollViewCell.h"
static NSString *const reuseffer = @"weiwei";

@interface SEGDoorCollViewCell ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *doorNameLabel;
@property (nonatomic, strong) UIImageView * highlightImageView;

@end

@implementation SEGDoorCollViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setupUI {
    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.highlightImageView];
    [self.contentView addSubview:self.doorNameLabel];
    
    UIImage *image = [UIImage imageNamed:SEGPicName(@"btn_opendoor_img")];
    CGFloat imageWidth = image.size.width;
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).mas_offset(self.bounds.size.width*0.5-imageWidth*0.5);
        make.top.mas_equalTo(self.contentView).mas_offset(15);
        make.height.equalTo(@(imageWidth));
        make.width.equalTo(@(imageWidth));
    }];
    
    [self.highlightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.centerX.centerY.equalTo(self.imageView);
    }];
    
    [self.doorNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.imageView.mas_bottom).mas_equalTo(8);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleBeginShakeAnimationNotification:) name:kSEGBeginDoorListShakeAnimationNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleEndShakeAnimationNotification:) name:kSEGEndDoorListShakeAnimationNotification object:nil];
}

- (void)setModel:(SEGNewMyDoorListModel *)model{
    self.doorNameLabel.text=model.name;
    [self resetSelectUI];
}

-(void)setIndex:(NSIndexPath *)index {
    _index=index;
    if (index.row < [SEGCommonConfig shared].newDoorSpecialNumber) {
        self.imageView.alpha = 0;
        self.highlightImageView.alpha = 1;
    } else {
        self.imageView.alpha = 1;
        self.highlightImageView.alpha = 0;
    }
    [self resetSelectUI];
}

- (void)changeImageWithIndexPath:(NSIndexPath *)indexPath {
    [UIView animateWithDuration:0.3 animations:^{
        if (indexPath.row < [SEGCommonConfig shared].newDoorSpecialNumber) {
            self.imageView.alpha = 0;
            self.highlightImageView.alpha = 1;
        } else {
            self.imageView.alpha = 1;
            self.highlightImageView.alpha = 0;
        }
    }];
    [self resetSelectUI];
}

- (void)updateSelectUI {
    self.imageView.layer.shadowColor = UIColor.blackColor.CGColor;
    self.imageView.layer.shadowOffset = CGSizeMake(4, 4);
    self.imageView.layer.shadowOpacity = 0.2;
    self.imageView.layer.shadowRadius = 8;
    
    self.highlightImageView.layer.shadowColor = UIColor.blackColor.CGColor;
    self.highlightImageView.layer.shadowOffset = CGSizeMake(4, 4);
    self.highlightImageView.layer.shadowOpacity = 0.2;
    self.highlightImageView.layer.shadowRadius = 8;
}

- (void)resetSelectUI {
    self.imageView.layer.shadowColor = UIColor.clearColor.CGColor;
    self.highlightImageView.layer.shadowColor = UIColor.clearColor.CGColor;
}

- (void)handleBeginShakeAnimationNotification:(NSNotification *)notificate {
    [self addAnimation];
}

- (void)handleEndShakeAnimationNotification:(NSNotification *)notificate {
    self.hidden = NO;
    [self removeAnimation];
}

- (void)addAnimation {
    if ([self.layer animationForKey:@"shake"]) {
        return;
    }
    CAKeyframeAnimation *animation = [[CAKeyframeAnimation alloc] init];
    animation.keyPath = @"transform.rotation";
    CGFloat shakeLevel = 4.0;
    animation.values = @[@(shakeLevel/180.0*M_PI*(-1.0)), @(shakeLevel/180.0*M_PI), @(shakeLevel/180.0*M_PI*(-1))];
    animation.repeatCount = INFINITY;
    animation.duration = 0.25;
    animation.removedOnCompletion = NO;
    [self.layer addAnimation:animation forKey:@"shake"];
}

- (void)removeAnimation {
    [self.layer removeAllAnimations];
}


- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView=[[UIImageView alloc]init];
        _imageView.image=[UIImage imageNamed:SEGPicName(@"btn_opendoor_img")];
    }
    return _imageView;
}

- (UIImageView *)highlightImageView {
    if (!_highlightImageView) {
        _highlightImageView = [[UIImageView alloc]init];
        _highlightImageView.image = [UIImage imageNamed:SEGPicName(@"btn_opendoor_img_major")];
        _highlightImageView.alpha = 0;
    }
    return _highlightImageView;
}

- (UILabel *)doorNameLabel{
    if (!_doorNameLabel) {
        _doorNameLabel=[UILabel new];
        _doorNameLabel.textAlignment = NSTextAlignmentCenter;
        _doorNameLabel.numberOfLines = 3;
        _doorNameLabel.font = [UIFont boldSystemFontOfSize:12];
        _doorNameLabel.textColor = kColor_1;
    }
    return _doorNameLabel;
}

+ (NSString *)reusefer{
    return reuseffer;
}

@end
